package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:YZN
 * @date:2022/12/30
 * @description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetmealMapper setmealMapper;

    @Override
    public R addCategory(Category category) {
        //todo 1. 参数校验
        if (category == null) {
            return R.error("参数错误");
        }
        //todo 1.1去重校验
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName, category.getName());
        Integer count = categoryMapper.selectCount(queryWrapper);
        if (count >= 1) {
            return R.error("该分类已存在");
        }
        //todo 2. 加入数据库
        categoryMapper.insert(category);
        return R.success("添加分类成功");
    }

    @Override
    public R findpage(Integer page, Integer pageSize) {
        //todo 1.根据sort升序
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);

        //todo 2.分页
        IPage<Category> pageInfo = new Page<>(page, pageSize);
        categoryMapper.selectPage(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    @Override
    public R deleteById(Long ids) {
        //todo 保证此分类下没有任何菜品
        //todo 判断此分类是否存在菜品
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId, ids);
        Integer dishCount = dishMapper.selectCount(dishQueryWrapper);

        if (dishCount > 0) {
            return R.error("该分类下还存在菜品");
        }
        //todo 判断此分类是否存在套餐
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, ids);
        Integer setmealCount = setmealMapper.selectCount(setmealQueryWrapper);
        if (setmealCount > 0) {
            return R.error("该分类下还存在套餐");
        }
        categoryMapper.deleteById(ids);
        return R.success("删除分类成功");
    }

    @Override
    public R updateCategory(Category category) {
        //todo 1. 参数校验
        if (category == null) {
            return R.error("参数错误");
        }
        //todo 1.1去重校验
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName, category.getName());
        queryWrapper.ne(Category::getId, category.getId());
        Integer count = categoryMapper.selectCount(queryWrapper);
        if (count > 0) {
            return R.error("该分类名已存在");
        }

        categoryMapper.updateById(category);
        return R.success("修改成功");
    }

    @Override
    public R list(Integer type) {
        /*//todo 1. 参数校验
        if (type == null) {
            return R.error("参数错误");
        }*/
        //todo 根据类型和排序查询对应的分类
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null, Category::getType, type);
        queryWrapper.orderByAsc(Category::getSort);
        List<Category> categories = categoryMapper.selectList(queryWrapper);

        return R.success(categories);
    }
}
